package org.zyl.util.number;

import java.text.DecimalFormat;

public class NumberUtil {
	private static DecimalFormat df = new DecimalFormat("0.00");
	/**
	 * 保留n位小数 n<10
	 */
	public static float keepThreeNumber(float f,int n) {
		String t = f+"000000000000000";
		t = t.substring(0,t.indexOf(".")+n+1);
		return Float.parseFloat(t);
	}
	public static double keepThreeNumber(double f,int n) {
		String t = f+"000000000000000";
		t = t.substring(0,t.indexOf(".")+n+1);
		return Double.parseDouble(t);
	}
	public static String keepTwoNumber(double f) {
		return df.format(f);
	}
	/**
	 * 获取随机数 length<30
	 * @author yzzhouyalei@foxmail.com
	 * @time 2019-01-11 10:52:08
	 */
	public static String getRandomNumber(int length) {
		StringBuffer sb = new StringBuffer();
		sb.append((int)((Math.random()*9+1))+Math.random()+"");
		sb.append((int)((Math.random()*9+1))+Math.random()+"");
		sb.append((int)((Math.random()*9+1))+Math.random()+"");
		
		String base = sb.toString().replace(".", "")+"0000000000000000000000000000000000000000000000000";
		return base.substring(0,length);
	}
}